package com.jacky.spi.factory;

import com.jacky.spi.SayHelloService;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: jacky
 * @date: 2018/6/20
 * Time: 16:17
 * Description:
 **/
public class DogSayHelloServiceFactory {

    private static volatile  SayHelloService sayHelloService = null;

    /**
     * 获得实现类单例对象
     * @return
     */
    public static SayHelloService getInstance(){
        if (sayHelloService == null){
            synchronized (SayHelloService.class){
                if (sayHelloService == null){
                    ServiceLoader<SayHelloService> load = ServiceLoader.load(SayHelloService.class);
                    Iterator<SayHelloService> iterator = load.iterator();
                    if (iterator.hasNext()){
                        sayHelloService = iterator.next();
                        return sayHelloService;
                    }
                }
            }
        }
        return  sayHelloService;
    }


}